package server;

import classes.Game;
import java.util.Calendar;

public class Admin {

    private String username;
    private String password;
    private Server server;
    private boolean access;
    private Calendar curr;

    Admin(String u, String p, Server s) {
        username = u;
        password = p;
        server = s;
        access = false;
    }

    public void login(String u, String p) {
        access = (u.equals(username) && p.equals(password));
        if (access) {
            System.out.println("Access Granted");

        }
        else {
            System.out.println("You do not have access");
        }
    }
    public void kill() {
        server.kill();
    }


    public void updateEventsLive(String statement, int index) {
        if (access) {
        Game changed = server.getLiveGames()[index];
        changed.events+=statement;
        server.setLiveGames(index,changed);
        }
       
        else {
            System.out.println("You do not have access.");
        }
    }


    public void updateLiveScore(int scoreA, int scoreB, int index) {
        if (access) {
        Game changed = server.getLiveGames()[index];
        changed.score1 = scoreA;
        changed.score2 = scoreB;
        server.setLiveGames(index, changed);
        }
        else {
            System.out.println("You do not have access.");
        }
    }

    public void updateGameScore (int gameNo, int score1, int score2) {
        curr = Calendar.getInstance();
        server.getDb().updateGameScore(gameNo, score1, score2, curr.HOUR+":"+curr.MINUTE);
    }

    public void updateGameEvents (int gameNo, String events, String updatetime) {
        server.getDb().updateGameEvents(gameNo, events, updatetime);
    }

    public void updateTeamPoints (boolean allTeams, int gameNo) {
        server.getDb().updateTeamPoints(allTeams, gameNo);
    }

    public void updateTeamRankings (boolean lastGameOnly, int lastPlayedGameNo) {
        server.getDb().updateTeamRankings(lastGameOnly, lastPlayedGameNo);
    }


    public String[] fillComboGames() {

        /*
         * Sample:
         * <game><16><1><FC Twente><FC Internazionale Milano><FC Twente Stadion,
         Enschede (NED)><2010-09-14><19:45:00><Pedro Proenca (POR)><0><0><null><null>
         <2><SV Werder Bremen><Tottenham Hotspur FC><Weserstadion, Bremen (GER)>
         <2010-09-14><19:45:00><Massimo Busacca (SUI)><0><0><null><null>
         */

        curr = Calendar.getInstance();
        String toParse = server.getDb().getGames(10, curr.YEAR+"-"+
                curr.MONTH+"-"+curr.DAY_OF_MONTH);
        String temp = toParse.substring(1, toParse.length() - 1);
        String parsed[] = temp.split("><");
        int n = Integer.parseInt(parsed[1]);
        int start = 3;
        String[] result = new String[n];
        for (int i = 0; i<n; i++) {
            result[i] = parsed[start]+"-"+parsed[start+1];
            start+=11;
        }

        System.out.println(result);

        return result;
    }
//    public void updateGameDate(int index) {
//        curr = Calendar.getInstance();
//        server.setGame(index,curr);
//    }



}
